package com.mingdao.lhy.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.pagehelper.Page;
import com.mingdao.lhy.biz.WishFillingBiz;
import com.mingdao.lhy.constant.Constant;
import com.mingdao.lhy.mapper.wish.AdmissionMapper;
import com.mingdao.lhy.mapper.wish.ArtSpecialtySignUpMapper;
import com.mingdao.lhy.mapper.wish.FiveYearPlanMapper;
import com.mingdao.lhy.mapper.wish.HighSchoolMapper;
import com.mingdao.lhy.mapper.wish.HighSchoolWishMapper;
import com.mingdao.lhy.mapper.wish.MiddleSchoolWishMapper;
import com.mingdao.lhy.mapper.wish.NinePlusFiveMapper;
import com.mingdao.lhy.mapper.wish.ScoreMapper;
import com.mingdao.lhy.mapper.wish.SignUpStudentMapper;
import com.mingdao.lhy.mapper.wish.SpecialProjectAndArtSpecialtyMapper;
import com.mingdao.lhy.mapper.wish.ThreeYearPlanMapper;
import com.mingdao.lhy.mapper.wish.WishMapper;
import com.mingdao.lhy.pojo.Admission;
import com.mingdao.lhy.pojo.ArtSpecialty;
import com.mingdao.lhy.pojo.FiveYearPlan;
import com.mingdao.lhy.pojo.HighSchool;
import com.mingdao.lhy.pojo.HighSchoolWish;
import com.mingdao.lhy.pojo.MiddleSchoolWish;
import com.mingdao.lhy.pojo.NinePlusFive;
import com.mingdao.lhy.pojo.Score;
import com.mingdao.lhy.pojo.SignUpStudent;
import com.mingdao.lhy.pojo.SpecialProject;
import com.mingdao.lhy.pojo.SpecialProjectAndArtSpecialty;
import com.mingdao.lhy.pojo.ThreeYearPlan;
import com.mingdao.lhy.pojo.WishFive;
import com.mingdao.lhy.pojo.WishFour;
import com.mingdao.lhy.pojo.WishOne;
import com.mingdao.lhy.pojo.WishSix;
import com.mingdao.lhy.pojo.WishThree;
import com.mingdao.lhy.pojo.WishTwo;
import com.mingdao.lhy.pojo.page.ArtSpecialtySignUp;

//@Service, @Controller，@Repository = {@Component + 一些特殊的functionality}
@Service
public class WishFillingServiceImpl implements WishFillingService {
	private static Logger log = LoggerFactory.getLogger(WishFillingServiceImpl.class);
	@Autowired
	private WishMapper wishMapper;
	@Autowired
	private HighSchoolMapper highSchoolMapper;
	@Autowired
	private SignUpStudentMapper signUpStudentMapper;
	@Autowired
	private SpecialProjectAndArtSpecialtyMapper specialProjectAndArtSpecialtyMapper;
	@Autowired
	private WishFillingBiz wishFillingBiz;
	@Autowired
	private FiveYearPlanMapper fiveYearPlanMapper;
	@Autowired
	private ThreeYearPlanMapper threeYearPlanMapper;
	@Autowired
	private NinePlusFiveMapper ninePlusFiveMapper;
	@Autowired
	private ArtSpecialtySignUpMapper artSpecialtyMapper;
	@Autowired
	private HighSchoolWishMapper highSchoolWishMapper;
	@Autowired
	private MiddleSchoolWishMapper middleSchoolWishMapper;
	@Autowired
	private ScoreMapper scoreMapper;
	@Autowired
	private AdmissionMapper admissionMapper;
	// @Autowired
	// private Wish wish;
	// @Autowired
	// private HighSchool highSchool;
	private ObjectMapper om = new ObjectMapper();

	// @Override
	// public void getHighSchoolByStudent(Student student, int pageNum, int size) {
	// PageHelper.startPage(pageNum, size);
	// // 判断学生情况，设置户籍、随迁、是否一类体制等信息
	// Page<HighSchool> highSchoolList = highSchoolMapper.listEpcByStudent(student);
	// try {
	// String str = om.writeValueAsString(highSchoolList);
	// } catch (JsonProcessingException e) {
	// // TODO Auto-generated catch block
	// e.printStackTrace();
	// }
	// }

	@Override
	public boolean isCanWillFilling() {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public Page<HighSchool> findByPage(int pageNum, int size) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public SignUpStudent getStudentByBmh(String bmh) {
		SignUpStudent student = signUpStudentMapper.getStudentByBmh(bmh);
		return student;
	}

	@Override
	@Transactional
	public int changePassword(SignUpStudent student) {
		int i = signUpStudentMapper.updatePasswordByPrimaryKey(student);
		return i;
	}

	@Override
	public List<HighSchool> getAllHighSchool() {
		List<HighSchool> highSchoolList = highSchoolMapper.getAllHighSchool();
		if (null == highSchoolList || highSchoolList.size() == 0) {
			log.warn("没有查到高中学校名单");
		}
		for (int i = 0; i < highSchoolList.size(); i++) {
			HighSchool highSchool = highSchoolList.get(i);
			log.debug(highSchool.getId());
			log.debug(highSchool.getXqdm());
			log.debug(highSchool.getXsdm());
			log.debug(highSchool.getXxdm());
			log.debug(highSchool.getXxjb());
			log.debug(highSchool.getXxmc());
			log.debug(highSchool.getZsjh());
		}

		return highSchoolList;
	}

	@Override
	@Transactional
	public int saveHighSchoolWish(HighSchoolWish wish) {
		int result = -1;
		if (null == wish || null == wish.getBmh() || wish.getBmh().isEmpty()) {
			return result;
		}
		/**
		 * 如果已报名信息存在，则更新，不存在则插入
		 */
		HighSchoolWish studentWish = highSchoolWishMapper.getOne(wish.getBmh());
		if (null == studentWish) {
			result = highSchoolWishMapper.insert(wish);
		} else {
			result = highSchoolWishMapper.updateByPrimaryKeySelective(wish);
		}

		return result;
	}

	/**
	 * 停用
	 */
	@Override
	public void getHighSchoolByStudent(SignUpStudent student) {

		/**
		 * 判断当前时间是哪个时间段，设置可以填报志愿的选项
		 */
		String lqpc = "";

		// TODO
		if ("1".equals(lqpc) && 1 == 1) {
			/**
			 * 如果是第一批次，筛选有特色项目的学校和艺体特长的学校
			 */
			/**
			 * 如果是第一批次，根据考生是否一类体制决定一类体制志愿是否可以填报
			 */

		} else if ("2".equals(lqpc)) {
			/**
			 * 否则就是第二批次，则第一批次设置为不可填报，只显示原来已填报的内容
			 */
			/**
			 * 第三批次和第二批次时间相同，根据考生是否一类体制决定一类体制志愿是否可以填报
			 */
			/**
			 * 根据户籍地确认户籍所在地，是否有随迁或随迁是否满三年确定随迁标志，报名号确定就读地
			 */
			// 如果随迁标志成立
			if ("1".equals(student.getSuiqian())) {
				// select * from gzdm where (@hujid=xqdm or kxsjh<>0 or
				// @jiudxsdm=substring(xsdm,1,4)) and (xxjb in ('sj','zj'))
			} else {
				// select * from gzdm where (@hujid=xqdm or kxsjh<>0) and (xxjb in ('sj','zj'))
			}

			/**
			 * 如果是一类体制学生，还可以再修改一类体制学校 TODO
			 */
			if (1 == 1) {
				// 获取考生户籍地
				String hjd = student.getHjd();

				if (null != Constant.puge_xichang.get(hjd)) {
					/**
					 * if @hujid in('513401','513428') --考生是普格、西昌籍 select * from gzdm where
					 * xxdm='7101009'
					 */

				} else if (null != Constant.dongwuxian.get(hjd)) {
					/**
					 * if @hujid in('513429','513430','513437','513436','513431')
					 * --考生是布拖、金阳、雷波、美姑、昭觉籍 select * from gzdm where xxdm='7111002'
					 */

				} else if (null != Constant.yuexi_ganluo_xide.get(hjd)) {
					/**
					 * if @hujid in('513434','513435','513432') --考生是越西、甘洛、喜德籍 select * from gzdm
					 * where xxdm='7112001'
					 */

				} else if (null != Constant.yanyuan_muli.get(hjd)) {
					/**
					 * if @hujid in('513422','513423') --考生是盐源、木里籍 select * from gzdm where
					 * xxdm='7103002'
					 */

				}

			}
		} else {
			/**
			 * 第三批次和第二批次时间相同，根据考生是否一类体制决定一类体制志愿是否可以填报
			 */
			// 如果随迁标志成立
			if ("1".equals(student.getSuiqian())) {
				// select * from gzdm where (@hujid=xqdm or @jiudxsdm=substring(xsdm,1,4)) and
				// (xxjb in ('pt','mb'))
			} else {
				// select * from gzdm where (@hujid=xqdm ) and (xxjb in ('pt','mb'))
			}
		}

	}

	@Override
	public WishOne getWishOneSchool(SignUpStudent student) {
		List<HighSchool> hsList = highSchoolMapper.getHighSchoolBySpecialProjectAndArtSpecialty();
		List<SpecialProject> featuredItems = specialProjectAndArtSpecialtyMapper.getSpecialProject();
		List<ArtSpecialty> artSpecialtys = specialProjectAndArtSpecialtyMapper.getArtSpecialty();

		WishOne wishOne = new WishOne();
		// wishOne.setOneClassOfSystems(oneClassSchool);
		wishOne.setFeaturedItems(featuredItems);
		wishOne.setSpecialtyOfArts(artSpecialtys);
		wishOne.setHsList(hsList);

		return wishOne;
	}

	/**
	 * 根据学生信息取第二批次高中学校
	 */
	@Override
	public WishTwo getWishTwoSchool(SignUpStudent signUpStudent) {
		/**
		 * 先排除一类体制学生
		 */
		// Boolean isOneClass = wishFillingBiz.isOneClassOfSystems(signUpStudent);
		// if (isOneClass) {
		// return null;
		// }
		/**
		 * 判断户籍地
		 */
		String hjd = signUpStudent.getHjd();// .substring(1, 6);
		if (null != hjd && !hjd.isEmpty() && hjd.length() >= 6) {
			hjd = hjd.substring(0, 6);
		} else {
			log.error("户籍地异常！");
			return null;
		}
		/**
		 * 就读地
		 */
		String jiudxsdm = signUpStudent.getJdd();
		if (null == jiudxsdm || jiudxsdm.isEmpty() || jiudxsdm.length() != 6) {
			log.error("报名号取就读地异常！");
			return null;
		}
		String suiqian = signUpStudent.getSuiqian();
		List<HighSchool> hsList = null;
		if (null != suiqian && "1".equals(suiqian)) {
			/**
			 * 随迁
			 */
			Map<String, String> map = new HashMap<String, String>();
			map.put("hjd", hjd);
			map.put("jdd", jiudxsdm);
			hsList = highSchoolMapper.getBlockTwoFollowUp(map);
		} else {
			/**
			 * 非随迁
			 */
			hsList = highSchoolMapper.getBlockTwoNotFollowUp(hjd);
		}

		WishTwo wishTwo = new WishTwo();
		wishTwo.setHsList(hsList);
		return wishTwo;
	}

	/**
	 * 根据学生信息取第三批次高中学校
	 */
	@Override
	public WishThree getWishThreeSchool(SignUpStudent signUpStudent) {
		/**
		 * 先排除一类体制学生
		 */
		// Boolean isOneClass = wishFillingBiz.isOneClassOfSystems(signUpStudent);
		// if (isOneClass) {
		// return null;
		// }
		/**
		 * 判断户籍地
		 */
		String hjd = signUpStudent.getHjd();// .substring(1, 6);
		if (null != hjd && !hjd.isEmpty() && hjd.length() >= 6) {
			hjd = hjd.substring(0, 6);
		} else {
			log.error("户籍地异常！");
			return null;
		}
		/**
		 * 根据报名号判断就读地
		 */
		String jiudxsdm = signUpStudent.getJdd();
		if (null == jiudxsdm || jiudxsdm.isEmpty() || jiudxsdm.length() != 6) {
			log.error("报名号取就读地异常！");
			return null;
		}
		String suiqian = signUpStudent.getSuiqian();
		List<HighSchool> hsList = null;
		if (null != suiqian && "1".equals(suiqian)) {
			/**
			 * 随迁
			 */
			Map<String, String> map = new HashMap<String, String>();
			map.put("hjd", hjd);
			map.put("jdd", jiudxsdm);
			hsList = highSchoolMapper.getBlockThreeFollowUp(map);
		} else {
			/**
			 * 非随迁
			 */
			hsList = highSchoolMapper.getBlockThreeNotFollowUp(hjd);
		}
		WishThree wishThree = new WishThree();
		wishThree.setHsList(hsList);
		return wishThree;
	}

	@Override
	public WishFour getWishFourSchool(SignUpStudent signUpStudent) {
		List<NinePlusFive> zzList = ninePlusFiveMapper.getBlockFour();
		WishFour wishFour = new WishFour();
		wishFour.setZzList(zzList);
		return wishFour;
	}

	@Override
	public WishFive getWishFiveSchool(SignUpStudent signUpStudent) {
		List<FiveYearPlan> fiveYearPlanNormal = fiveYearPlanMapper.getBlockFiveNormal();
		List<FiveYearPlan> fiveYearPlanNotNormal = fiveYearPlanMapper.getBlockFiveNotNormal();
		WishFive wishFive = new WishFive();
		wishFive.setNormalList(fiveYearPlanNormal);
		wishFive.setNotNormalList(fiveYearPlanNotNormal);
		return wishFive;
	}

	@Override
	public WishSix getWishSixSchool(SignUpStudent signUpStudent) {
		List<ThreeYearPlan> threeYearList = threeYearPlanMapper.getBlockThree();
		WishSix wishSix = new WishSix();
		wishSix.setThreeYearList(threeYearList);
		return wishSix;
	}

	/**
	 * 艺体报名数据保存
	 */
	@Override
	@Transactional
	public int saveArtSpecialtyYt(ArtSpecialtySignUp artSpecialtySignUp) {
		/**
		 * 先根据报名号查询学生的基本信息
		 */
		SignUpStudent student = signUpStudentMapper.getStudentByBmh(artSpecialtySignUp.getBmh());
		/**
		 * 从ls18bmk取数据对艺体特色报名信息进行赋值完善
		 */
		artSpecialtySignUp.setSfzh(student.getSfzh());
		artSpecialtySignUp.setXm(student.getXm());
		artSpecialtySignUp.setXb(student.getXb());
		artSpecialtySignUp.setMz(student.getMz());
		artSpecialtySignUp.setZzmm(student.getZzmm());
		artSpecialtySignUp.setCsrq(student.getCsrq());
		artSpecialtySignUp.setTxdz(student.getTxdz());
		artSpecialtySignUp.setByzxdm(student.getByzxdm());
		artSpecialtySignUp.setXjh(student.getXjh());
		artSpecialtySignUp.setLxdh(student.getLxdh());

		/**
		 * 保存报名信息
		 */
		int result = -1;
		try {
			/**
			 * 先查询一下报名数据是否存在，不存在则插入，存在则更新
			 */
			ArtSpecialtySignUp tmpObj = artSpecialtyMapper.selectByPrimaryKeyYt(artSpecialtySignUp.getBmh());
			if (null == tmpObj) {
				/**
				 * 不存在，插入
				 */
				result = artSpecialtyMapper.insertYt(artSpecialtySignUp);
			} else {
				/**
				 * 存在，更新
				 */
				result = artSpecialtyMapper.updateByPrimaryKeyYt(artSpecialtySignUp);
			}

		} catch (Exception e) {
			log.error(e.getMessage());
		}
		if (result != 1) {
			log.error("艺体特色报名数据保存异常！");
		}
		return result;
	}

	/**
	 * 艺体报名数据删除
	 */
	@Override
	@Transactional
	public int deleteArtSpecialtyYt(ArtSpecialtySignUp artSpecialtySignUp) {
		int result = artSpecialtyMapper.deleteByPrimaryKeyYt(artSpecialtySignUp.getBmh());
		return result;
	}

	/**
	 * 特色报名数据保存
	 */
	@Override
	@Transactional
	public int saveArtSpecialtyTs(ArtSpecialtySignUp artSpecialtySignUp) {
		/**
		 * 先根据报名号查询学生的基本信息
		 */
		SignUpStudent student = signUpStudentMapper.getStudentByBmh(artSpecialtySignUp.getBmh());
		/**
		 * 从ls18bmk取数据对艺体特色报名信息进行赋值完善
		 */
		artSpecialtySignUp.setSfzh(student.getSfzh());
		artSpecialtySignUp.setXm(student.getXm());
		artSpecialtySignUp.setXb(student.getXb());
		artSpecialtySignUp.setMz(student.getMz());
		artSpecialtySignUp.setZzmm(student.getZzmm());
		artSpecialtySignUp.setCsrq(student.getCsrq());
		artSpecialtySignUp.setTxdz(student.getTxdz());
		artSpecialtySignUp.setByzxdm(student.getByzxdm());
		artSpecialtySignUp.setXjh(student.getXjh());
		artSpecialtySignUp.setLxdh(student.getLxdh());

		/**
		 * 保存报名信息
		 */
		int result = -1;
		try {
			/**
			 * 先查询一下报名数据是否存在，不存在则插入，存在则更新
			 */
			ArtSpecialtySignUp tmpObj = artSpecialtyMapper.selectByPrimaryKeyTs(artSpecialtySignUp.getBmh());
			if (null == tmpObj) {
				/**
				 * 不存在，插入
				 */
				result = artSpecialtyMapper.insertTs(artSpecialtySignUp);
			} else {
				/**
				 * 存在，更新
				 */
				result = artSpecialtyMapper.updateByPrimaryKeyTs(artSpecialtySignUp);
			}

		} catch (Exception e) {
			log.error(e.getMessage());
		}
		if (result != 1) {
			log.error("艺体特色报名数据保存异常！");
		}
		return result;
	}

	/**
	 * 州教育局获取所有特色项目及艺体特长报名学生信息
	 */
	@Override
	public List<ArtSpecialtySignUp> getAllArtSpeciatlyStudent() {
		List<ArtSpecialtySignUp> artSpecialtySignUpList = artSpecialtyMapper.list();
		if (null == artSpecialtySignUpList || artSpecialtySignUpList.size() == 0) {
			log.warn("没有找到报名的学生信息");
		}
		return artSpecialtySignUpList;
	}

	/**
	 * 根据学生报名号获取一名特色项目及艺体特长报名学生信息
	 */
	@Override
	public ArtSpecialtySignUp getArtSpeciatlyStudentByBmh(String bmh) {
		ArtSpecialtySignUp artSpecialtySignUpYt = artSpecialtyMapper.selectByPrimaryKeyYt(bmh);
		ArtSpecialtySignUp artSpecialtySignUpTs = artSpecialtyMapper.selectByPrimaryKeyTs(bmh);
		if (null != artSpecialtySignUpYt) {
			if (null != artSpecialtySignUpTs) {
				artSpecialtySignUpYt.setGzxxdmts(artSpecialtySignUpTs.getGzxxdmts());
				artSpecialtySignUpYt.setGzxxmcts(artSpecialtySignUpTs.getGzxxmcts());
				artSpecialtySignUpYt.setTszydm(artSpecialtySignUpTs.getTszydm());
				artSpecialtySignUpYt.setTszymc(artSpecialtySignUpTs.getTszymc());
				artSpecialtySignUpYt.setTsxmcl(artSpecialtySignUpTs.getTsxmcl());
				artSpecialtySignUpYt.setTskhcj(artSpecialtySignUpTs.getTskhcj());
			}
		} else if (null != artSpecialtySignUpTs) {
			return artSpecialtySignUpTs;
		}
		return artSpecialtySignUpYt;
	}

	@Override
	@Transactional
	public int saveMiddleSchoolWish(MiddleSchoolWish wish) {
		int result = -1;
		MiddleSchoolWish middleWish = middleSchoolWishMapper.getOne(wish.getBmh());
		/**
		 * 如果已报名信息存在，则更新，不存在则插入
		 */
		if (null == middleWish) {
			result = middleSchoolWishMapper.insert(wish);
		} else {
			result = middleSchoolWishMapper.updateByPrimaryKeySelective(wish);
		}

		return result;
	}

	@Override
	public HighSchoolWish getOneTwoThreeStudentByBmh(String bmh) {
		HighSchoolWish highSchoolWish = highSchoolWishMapper.getOne(bmh);
		return highSchoolWish;
	}

	@Override
	public MiddleSchoolWish getFourFiveSixStudentByBmh(String bmh) {
		MiddleSchoolWish middleSchoolWish = middleSchoolWishMapper.getOne(bmh);
		return middleSchoolWish;
	}

	/**
	 * 初始化学生密码
	 * 
	 * 默认为身份证号后8位
	 */
	@Override
	@Transactional
	public int initStudentPassword(String bmh) {
		SignUpStudent signUpStudent = signUpStudentMapper.getStudentByBmh(bmh);

		/**
		 * 检查返回学生信息
		 */
		if (null == signUpStudent) {
			log.error("没有找到对应报名号的学生信息=" + bmh);
			return -1;
		}

		String sfzh = signUpStudent.getSfzh();

		/**
		 * 检查身份证号
		 */
		if (null == sfzh || sfzh.isEmpty()) {
			log.error("");
			return -1;
		}
		/**
		 * 截取身份证最后8位
		 */
		String passowrd = sfzh.substring(10, 18);

		signUpStudent.setPassword(passowrd);

		int result = signUpStudentMapper.updatePasswordByPrimaryKey(signUpStudent);
		return result;
	}

	@Override
	public Score queryExamResultByBmh(String bmh) {

		Score examResult = scoreMapper.getOne(bmh);
		return examResult;
	}

	@Override
	public List<Score> queryAllExamResult() {
		List<Score> examResults = scoreMapper.list();
		return examResults;
	}

	@Override
	public Admission queryAdmissionByBmh(String bmh) {

		Admission admission = admissionMapper.getOne(bmh);
		return admission;
	}

	/**
	 * 根据学校代码获取高中专业
	 */
	@Override
	public List<SpecialProjectAndArtSpecialty> getMajorByHighSchoolId(String schoolId) {
		List<SpecialProjectAndArtSpecialty> highSchools = highSchoolMapper.getMajorBySchool(schoolId);
		return highSchools;
	}

	/**
	 * 根据学校代码获取五年计划学校师范类专业
	 */
	@Override
	public List<FiveYearPlan> getNormalMajorFiveYearPlanBySchoolId(String schoolId) {
		List<FiveYearPlan> fiveYearPlans = fiveYearPlanMapper.getMajorBySchoolNormal(schoolId);
		return fiveYearPlans;
	}

	/**
	 * 根据学校代码获取五年计划学校非师范类专业
	 */
	@Override
	public List<FiveYearPlan> getNotNormalMajorFiveYearPlanBySchoolId(String schoolId) {
		List<FiveYearPlan> fiveYearPlans = fiveYearPlanMapper.getMajorBySchoolNotNormal(schoolId);
		return fiveYearPlans;
	}

	/**
	 * 根据学校代码获取三年计划学校专业
	 */
	@Override
	public List<ThreeYearPlan> getMajorThreeYearPlanBySchoolId(String schoolId) {
		List<ThreeYearPlan> threeYearPlans = threeYearPlanMapper.getMajorBySchool(schoolId);
		return threeYearPlans;
	}

	/**
	 * 根据学校代码获取9+5学校专业
	 */
	@Override
	public List<NinePlusFive> getMajorNinePlusFiveBySchoolId(String schoolId) {
		List<NinePlusFive> ninePlusFive = ninePlusFiveMapper.getMajorBySchool(schoolId);
		return ninePlusFive;
	}

	/**
	 * 根据传入的学校代码集返回对应的高中学校信息
	 */
	@Override
	public List<HighSchool> getOneClassOfSystems(List<String> xxdms) {
		List<HighSchool> highSchools = highSchoolMapper.getOneClassOfSystems(xxdms);
		return highSchools;
	}

	@Override
	public HighSchool getSchoolByJdd(String jdd) {
		HighSchool highSchool = highSchoolMapper.getHighSchoolByJdd(jdd);
		return highSchool;
	}

}
